package org.mics.sitemsg.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.mics.core.page.PageInfo;
import org.mics.core.response.PageDataResponse;
import org.mics.lang.bean.BeanConvertUtil;
import org.mics.sitemsg.VO.SiteMsgVO;
import org.mics.sitemsg.entity.SiteMsgDO;
import org.mics.sitemsg.enums.SiteMsgState;
import org.mics.sitemsg.query.SiteMsgQuery;
import org.mics.sitemsg.repository.SiteMsgRepository;
import org.mics.sitemsg.service.SiteMsgService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

/**
 * 站内信
 * @author mics
 * @date 2021-11-8
 * @version  1.0
 */
@Service
public class SiteMsgServiceImpl implements SiteMsgService{
	@Resource
	private SiteMsgRepository siteMsgRepository;
	
	@Override
	public String save(String title, String content, String receiveUserId, String jumpUrl) {
		SiteMsgDO siteMsgDO = new SiteMsgDO();
		siteMsgDO.setTitle(title);
		siteMsgDO.setContent(content);
		siteMsgDO.setReceiveUserId(receiveUserId);
		siteMsgDO.setState(SiteMsgState.NOT_READ);
		siteMsgDO.setJumpUrl(jumpUrl);
		siteMsgRepository.save(siteMsgDO);
		return siteMsgDO.getId();
	}

	@Override
	public String save(String title, String content, String receiveUserId) {
		SiteMsgDO siteMsgDO = new SiteMsgDO();
		siteMsgDO.setTitle(title);
		siteMsgDO.setContent(content);
		siteMsgDO.setReceiveUserId(receiveUserId);
		siteMsgDO.setState(SiteMsgState.NOT_READ);
		siteMsgRepository.save(siteMsgDO);
		return siteMsgDO.getId();
	}
	
	
	@Override
	public PageDataResponse<SiteMsgVO> listByPage(SiteMsgQuery siteMsgQuery, String userId) {
		Specification<SiteMsgDO> specification = new Specification<SiteMsgDO>() {
			private static final long serialVersionUID = -8785309034575411806L;
            @Override
            public Predicate toPredicate(Root<SiteMsgDO> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];
				lstPredicates.add(cb.equal(root.get("receiveUserId").as(String.class),userId));
				if(siteMsgQuery.getState() != null) {
					lstPredicates.add(cb.equal(root.get("state").as(Integer.class),siteMsgQuery.getState().ordinal()));
				}
				return cb.and(lstPredicates.toArray(arrayPredicates));
            }
	   };
	  Pageable pageable = PageRequest.of(siteMsgQuery.getPageIndex()-1> 0?siteMsgQuery.getPageIndex()-1:0,siteMsgQuery.getPageSize(), Sort.Direction.DESC, "createDate");
	  //查询
	  Page<SiteMsgDO> page = siteMsgRepository.findAll(specification,pageable);
	  PageInfo pageInfo = PageInfo.create(page);
	  List<SiteMsgVO> siteMsgVOs = BeanConvertUtil.convertList(page.getContent(), SiteMsgVO.class);
	  return new PageDataResponse<SiteMsgVO>(pageInfo, siteMsgVOs);
	}

	@Override
	public SiteMsgDO info(String id) {
		SiteMsgDO siteMsgDO = siteMsgRepository.getById(id);
		siteMsgDO.setState(SiteMsgState.READ);
		siteMsgRepository.save(siteMsgDO);
		return siteMsgDO;
	}

	@Override
	@Transactional
	public void remove(String id) {
		siteMsgRepository.deleteById(id);
	}

	@Override
	public Long noReadNumber(String userId) {
		Long number = siteMsgRepository.countByReceiveUserIdAndState(userId,SiteMsgState.NOT_READ);
		return number;
	}

	

}
